﻿using Microsoft.InformationProtectionAndControl;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudStorageLight.Core
{
    public static class RmsUtil
    {
        public static void Init()
        {
                SafeNativeMethods.IpcInitialize();
                SafeNativeMethods.IpcSetAPIMode(APIMode.Server);
        }

        public static string EncryptFile(ConnectionInfo info, SymmetricKeyCredential symmetricKeyCred, string filePath, string templateId)
        {

            string filepath = SafeFileApiNativeMethods.IpcfEncryptFile(filePath, templateId,
            SafeFileApiNativeMethods.EncryptFlags.IPCF_EF_FLAG_KEY_NO_PERSIST, true, false, true, null, symmetricKeyCred);
            return filepath;
        }

        public static string DecryptFile(ConnectionInfo info, SymmetricKeyCredential symmetricKeyCred, string filePath)
        {
            string filepath = SafeFileApiNativeMethods.IpcfDecryptFile(filePath,
            SafeFileApiNativeMethods.DecryptFlags.IPCF_DF_FLAG_DEFAULT, true, false, true, null, symmetricKeyCred);
            return filepath;
        }

        public static Collection<TemplateInfo> GetTemplateList(ConnectionInfo info, SymmetricKeyCredential symmetricKeyCred)
        {
            return SafeNativeMethods.IpcGetTemplateList(null, true, false, false, true, null, null, symmetricKeyCred);
        }

        public static bool IsFileEncrypted(string inputFile)
        {
            return SafeFileApiNativeMethods.IpcfIsFileEncrypted(inputFile) != SafeFileApiNativeMethods.FileEncryptedStatus.IPCF_FILE_STATUS_DECRYPTED;
        }

        public static string EncryptFileForMailUser(ConnectionInfo info, SymmetricKeyCredential symmetricKeyCred, string filePath, string username)
        {

            Collection<string> rights = new Collection<string>();
            rights.Add("READ");
            rights.Add("PRINT");
            Collection<UserRights> userRights = new Collection<UserRights>();
            userRights.Add(new UserRights(UserIdType.Email, username, rights));
            string templateName = "Confidential - Read and Print only";
            string templateDescription = "Confidential information - Read and Print allowed";
             string issuerDisplayName = "Contoso web page";
            // Get the available issuers of rights policy templates.
            // The available issuers is a list of RMS servers that this user has already contacted.
             Collection<TemplateIssuer> templateIssuers =
            SafeNativeMethods.IpcGetTemplateIssuerList(info, false, true, false, false, null, symmetricKeyCred);

            // Create the policy and associate the chosen user rights with it
            SafeInformationProtectionLicenseHandle handle = SafeNativeMethods.IpcCreateLicenseFromScratch(templateIssuers.ElementAt(0));
            SafeNativeMethods.IpcSetLicenseUserRightsList(handle, userRights);
            SafeNativeMethods.IpcSetLicenseDescriptor(handle, new TemplateInfo(null, CultureInfo.CurrentCulture, templateName, templateDescription, issuerDisplayName, false));
            // Encrypt the file using the ad-hoc policy
            string encryptedFilePath = SafeFileApiNativeMethods.IpcfEncryptFile(filePath,handle,
            SafeFileApiNativeMethods.EncryptFlags.IPCF_EF_FLAG_KEY_NO_PERSIST, true, false, true, null, symmetricKeyCred);
            return encryptedFilePath;
        }

    }
}
